<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    //创建一个人类
    var Human = function (param) {
        this.skill = param && param.skill || '保密';
        this.hobby = param && param.hobby || '保密';
    }

    Human.prototype = {
        getSkill: function () {
            return this.skill;
        },
        getHobby: function () {
            return this.hobby;
        }
    }

    //创建姓名类
    var Named = function (name) {
        let that = this;
        //构造器函数,解析姓与名(new构造实例时即刻执行此闭包函数)
        (function (name, that) {
            //闭包函数this指向window
            that.wholeName = name;//全名赋值
            if (name.indexOf(' ') > -1) {
                that.firstName = name.slice(0, name.indexOf(' '));
                that.secondName = name.slice(name.indexOf(' '));
            }
        })(name, that);
    };
    //创建职位类
    var Work = function (work) {
        let that = this;
        //构造器函数,通过传入的职位类设置相应的职位及描述
        (function (work, that) {
            switch (work) {
                case 'code':
                    that.work = '软件工程师';
                    that.workDescribe = '敲代码';
                    break;
                case 'UI':
                case 'UE':
                    that.work = '设计师';
                    that.workDescribe = '艺术设计';
                    break;
                case 'teacher':
                    that.work = '教师';
                    that.workDescribe = '教书';
                    break;
                default:
                    that.work = work;
                    that.workDescribe = '不描述';
            }
        })(work, that);
    }

    Work.prototype = {
        getWork: function () {
            return this.work;
        },
        setWork: function (work) {
            this.work = work;
        },
        getWorkDescribe: function () {
            return this.workDescribe;
        },
        setWorkDescribe: function (desc) {
            this.workDescribe = desc;
        }
    };

    //创建应聘者builder
    var Applicant = function (name, work, param) {
        let person = new Human(param);
        person.name = new Named(name);
        person.work = new Work(work);
        return person;
    }
</script>
<script>
    //测试用例
    var person = new Applicant('小 明', 'code');

    console.log(person.getSkill());
    console.log(person.getHobby());
    console.log(person.name.firstName);
    console.log(person.work.getWork());
    console.log(person.work.getWorkDescribe());
    person.work.setWorkDescribe('我每天都在快乐的编程学习')
    console.log(person.work.getWorkDescribe());
    console.log(person instanceof Applicant);
    console.log(person instanceof Human);
    console.log(person instanceof Named);
</script>
<script>
    function Person() {

    }

    Person.prototype = {
        setName: function (name) {
            this.name = name;
            return this;
        },
        setAge: function (age) {
            this.age = age;
            return this;
        },
        setSex: function (sex) {
            this.sex = sex;
            return this;
        }
    }

    Person.create = function () {
        return new Person();
    }

    let p = Person.create().setName("aitao").setSex("男").setAge(18);
    console.log(p);
</script>
</body>
</html>